package org.weed.gadgets.audio.asf;

import java.math.BigInteger;

import org.weed.gadgets.util.NioFileReader;

public class ASFObject {
	protected static final int HEADER_SIZE = 24;
	protected final GUID_TYPE type;
	protected final GUID guid;
	protected final long size;
	protected final int headerSize;
	protected final long index;
	protected final long dataIndex;
	protected byte[] data;
	
	public ASFObject(GUID guid, long size, long index, int headerSize) {
		type = GUID_TYPE.HEADER;
		this.guid = guid;
		this.size = size;
		this.headerSize = headerSize;
		this.index = index;
		this.dataIndex = index + headerSize;
	}
	
	public ASFObject loadData(NioFileReader reader) {
		data = reader.read(dataIndex, (int)(size - headerSize));
		return this;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("GUID: ").append(guid).append("\n")
				.append("Size: ").append(size).append("\n")
				.append("Index: ").append(index);
		return builder.toString();
	}
	
	public static ASFObject getASFObject(NioFileReader reader, long position) {
		GUID guid = GUID.parseGUID(reader.read(position, ASFConstants.GUID));
		BigInteger number = new BigInteger(1, reader.reverseRead(ASFConstants.QWORD));
		long size = number.longValue();
		return new ASFObject(guid, size, position, HEADER_SIZE);
	}
}
